<?php
//USER_ID PLATFORM_ID都在main函数中定义
function messageopt($messageStr)
{
  $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[%s]]></MsgType>
        <Content><![CDATA[%s]]></Content>
        </xml>";
    $msgType = "text";  
    $time = time();
    echo sprintf($textTpl, USER_ID, PLATFORM_ID, $time, $msgType, $messageStr);
    $msgType = "transfer_customer_service";  
    $time = time();
    echo sprintf($textTpl, USER_ID, PLATFORM_ID, $time, $msgType, $messageStr);
}
function textopt($contentStr)
{
	$textTpl = "<xml>
				<ToUserName><![CDATA[%s]]></ToUserName>
				<FromUserName><![CDATA[%s]]></FromUserName>
				<CreateTime>%s</CreateTime>
				<MsgType><![CDATA[%s]]></MsgType>
				<Content><![CDATA[%s]]></Content>
				</xml>";
	$msgType = "text";	
	$time = time();
	echo sprintf($textTpl, USER_ID, PLATFORM_ID, $time, $msgType, $contentStr);
}
function imageopt($PicUrl)
{
    $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <PicUrl><![CDATA[%s]></PicUrl> 
                </xml>";
    $msgType = "image";	
    $time = time();
    echo sprintf($textTpl, USER_ID, PLATFORM_ID, $time, $msgType, $PicUrl);	
}
function turnNew($arr)
{
    $msgType = "news";
    $time = time();
    $count=0;
    foreach($arr as $row)
    {
        if($row['cond']==1)
        {
            $itemTpl.="<item>
                      <Title><![CDATA[".$row['Title']."]]></Title> 
                      <Description><![CDATA[".$row['Description']."]]></Description>
                      <PicUrl><![CDATA[".$row['PicUrl']."]]></PicUrl>
                      <Url><![CDATA[".$row['Url']."]]></Url>
                      </item>";
        }
        else
        {
            $itemTpl.="<item>
                      <Title><![CDATA[".$row."]]></Title> 
                      <Description><![CDATA[]]></Description>
                      <PicUrl><![CDATA[]]></PicUrl>
                      </item>"; 
        }
            $count++;   
    }
    $textTpl.="<xml>
               <ToUserName><![CDATA[%s]]></ToUserName>
               <FromUserName><![CDATA[%s]]></FromUserName>
               <CreateTime>%s</CreateTime>
               <MsgType><![CDATA[%s]]></MsgType>
               <ArticleCount>$count</ArticleCount>                                                                                       
               <Articles>".$itemTpl."</Articles>
               </xml>";
    echo sprintf($textTpl, USER_ID, PLATFORM_ID, $time, $msgType);
}
function weather()
{

    $arr=file_get_contents('http://api.map.baidu.com/telematics/v3/weather?location=%E5%B9%BF%E5%B7%9E&output=json&ak=5MCjPFrxupYNe7PjhPa2Am3y');
    $arr=json_decode($arr);
    $date=$arr->date;
    $data=$arr->results[0]->weather_data;
    $tmp=explode(' ',$data[0]->date);
    $data[0]->date=$tmp[0];
    if(!empty($data))
    {
        $msgType = "news";
        $time = time();
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                                                            
                    <ArticleCount>5</ArticleCount>
                    <Articles>
                    <item>
                    <Title><![CDATA[权小益*广州天气预报]]></Title> 
                    <Description><![CDATA[]]></Description>
                    <PicUrl><![CDATA[http://1.scutrfa.sinaapp.com/wuxietest/GZTower.jpg]]></PicUrl>
                    </item>";
        for($i=0;$i<4;$i++)
        {
            $textTpl.="
                    <item>
                    <Title><![CDATA[".$data[$i]->date."\n".$data[$i]->temperature." ".$data[$i]->weather."]]></Title> 
                    <Description><![CDATA[]]></Description>
                    <PicUrl><![CDATA[".$data[$i]->dayPictureUrl."]]></PicUrl>
                    </item>";
        }
        $textTpl.="</Articles>
                    </xml>";
        echo sprintf($textTpl, USER_ID, PLATFORM_ID, $time, $msgType);
    }
    else textopt('信息获取失败');
}
?>